<template>
    <div class="login">
      
       <el-form ref="form" :model="form" class="container" :rules="rules">
             <el-form-item>
       <div class="avatar">
           <img src="../assets/avatar.jpg">
       </div>
        </el-form-item>
       <el-form-item prop="username">
         <el-input v-model="form.username" placeholder="账户"  prefix-icon="myicon myicon-user"></el-input>
        </el-form-item>
    <el-form-item prop="password">
        <el-input v-model="form.password" placeholder="密码" type="password" prefix-icon="myicon myicon-key"></el-input>
    </el-form-item>
       <el-form-item>
       <el-button type="primary"  class="login-btn" @click="loginSubmit('form')">登录</el-button>
        </el-form-item>
       </el-form>
        
 
    </div>
</template>
<script>
import {checkUser} from '@/api/index.js'
export default {
  data(){
       return {
           form:{
              username:'',
              password:''
           },
           rules:{
             username:[
                   { required: true, message: '请输入用户名', trigger: 'blur' },
                   ],
                password:[
                   { required: true, message: '请输入密码', trigger: 'blur' },
                   ]  
           }
                  }
  },
           methods:{
              loginSubmit(formName){
                this.$refs[formName].validate((valid)=>{
                if (valid) {
                  checkUser(this.form).then(res=>{
                     if(res.meta.status===200){
                          localStorage.setItem('mytoken',res.data.token); 
                          this.$store.commit('setUsername',res.data.username)
                         this.$router.push({name:'Home'});
                     }
                     else{
                        this.$message({
                         message:res.meta.msg,
                          type: 'warning'
        });
                     }

                  })
                  }
                 else {
                       console.log('error submit!');
           }
              })
           }}
}
//  promise在上一个.then返回的函数在下一个.then能够接受
</script>
